package com.rj.core.http;

import javax.net.ssl.*;
import java.security.cert.X509Certificate;

/**
 * SSL证书处理
 *
 * @author YFX_RANLUBO
 * @date 2025/06/20 14:19
 **/
public class SSLUtils {

    private static SSLContext sslContext;

    /**
     * 获得SSLSocketFactory
     * @return
     * @throws Exception
     */
    public static SSLSocketFactory getSSLSocketFactory() throws Exception
    {
        if(sslContext == null){
            sslContext = SSLContext.getInstance("TLS");
            TrustManager[] tm = {new TrustAnyTrustManager()};
            sslContext.init(null, tm, new java.security.SecureRandom());
        }

         return  sslContext.getSocketFactory() ;
    }
    /**
     * TrustAnyHostnameVerifier验证器
     * @return
     */
    public static TrustAnyHostnameVerifier getTrustAnyHostnameVerifier() {
        return  new TrustAnyHostnameVerifier() ;
    }
    /**
     * TrustAnyTrustManager管理器
     * @return
     */
    private static class TrustAnyTrustManager implements X509TrustManager
    {
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType)
        {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType)
        {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers()
        {
            return new X509Certificate[] {};
        }
    }

    private static class TrustAnyHostnameVerifier implements HostnameVerifier
    {
        @Override
        public boolean verify(String hostname, SSLSession session)
        {

            return true;
        }
    }
}
